package com.marvels.approve.process.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.marvels.approve.process.dao.IFaceSigConfiguractionDao;
import com.marvels.approve.process.dao.ITimeConfHistoryDao;
import com.marvels.approve.process.dto.FaceSigConfigurationDTO;
import com.marvels.approve.process.dto.SysUserDTO;
import com.marvels.approve.process.dto.TimeConfHistoryDTO;
import com.marvels.approve.process.service.IFaceSigAppointmentService;
import com.marvels.approve.process.service.IFaceSigConfigurationService;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;

@Service
public class FaceSigConfigurationServiceImpl implements IFaceSigConfigurationService {
	@Autowired
	private IFaceSigConfiguractionDao faceSigConfiguractionDao;
	@Autowired
	private ITimeConfHistoryDao timeConfHistoryDao;
	@Autowired
	private IFaceSigAppointmentService faceSigAppointmentService;

	/**
	 * 
	 * 通过版本ID新建参数配置明细
	 */
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	@Override
	public int addFaceSigConfiguration(TimeConfHistoryDTO timeConfHistoryDto) throws MarvelsBusinessException {
		UserDTO user = CacheUtils.getCacheUser();  //从缓存中读取到用户
		String userCode =user.getUserCode() == null?MarvelsConstant.SYSTEM_USER : user.getUserCode();
		
		if(timeConfHistoryDto.getId() == 0){
			throw new MarvelsBusinessException("版本ID为空" + timeConfHistoryDto.getId());
		}
		List<FaceSigConfigurationDTO> configurations = timeConfHistoryDto.getFaceSigConfigurationList();
		if(CollectionUtils.isNotEmpty(configurations) ){
			for (FaceSigConfigurationDTO dto : configurations) {
				dto.setCreateBy(userCode);
				dto.setUpdateBy(userCode);
				dto.setTimeConfHistoryId(timeConfHistoryDto.getId());
				//设置默认值
				if(dto.getFacesigCount()==0||dto.getFacesigCount()==null){
					 List<SysUserDTO> users = faceSigAppointmentService.selectFaceSignUsers();
					 dto.setFacesigCount(users.size());
					 dto.setFacesigNumber(users.size());
				}
				faceSigConfiguractionDao.insertFaceSigAppointment(dto);
			}
		}
		return 0;
	}

	/**
	 * 
	 * 获取流程状态
	 */
	@Override
	public List<Map<String, Object>> selectCaseNode() throws MarvelsBusinessException {
		return faceSigConfiguractionDao.selectCaseNode();
	}
	
	/**
	 * 
	 * 获取面签预约时间起和预约时间止
	 * @return
	 */
	@Override
	public Page<FaceSigConfigurationDTO> selectStartTime() throws MarvelsBusinessException{
		Integer historyId = timeConfHistoryDao.queryTimeConfByDate(new Date());
		return faceSigConfiguractionDao.selectStartTime(historyId);
	}

	@Override
	public FaceSigConfigurationDTO selectByPrimaryKey(Integer timeId) throws MarvelsBusinessException {
		// TODO Auto-generated method stub
		return faceSigConfiguractionDao.selectByPrimaryKey(timeId);
	}
	
	
	

}
